package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.text.Html;
import android.view.View;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.view.MyLinearLayoutManager;
import com.hisan.freeride.databinding.ExchangeBinding;
import com.hisan.freeride.home.adapter.ExChangeAdapter;
import com.hisan.freeride.home.model.Exchange;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/6/26
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：爱心币兑换记录
 */
public class ExchangeActivity extends BaseActivity<ExchangeBinding> {
    private int pageNumber=1;
    private ExChangeAdapter priceAdapter;
    private String str="赶紧去<font color='#FF9C00'  size:'28px'><small>"+"兑换"+"</small></font>吧";

    @Override
    protected int getLayoutId() {
        return R.layout.exchange;
    }

    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        mBinding.name.setText(Html.fromHtml(str));
        priceAdapter=new ExChangeAdapter(new ArrayList<>(0));
        mBinding.rv.setLayoutManager(new MyLinearLayoutManager(this));
        mBinding.rv.setAdapter(priceAdapter);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.shape_drive_line));
        mBinding.rv.addItemDecoration(dividerItemDecoration);

    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        priceAdapter.setButtonClickListener(exchange -> {
            if (!CollectionUtils.isNullOrEmpty(exchange.getCodedata())){
                Bundle bundle=new Bundle();
                bundle.putString("code",exchange.getCodedata().getTakecode());
                bundle.putString("site",exchange.getCodedata().getAddress());
                startActivityForResult(CodeActivity.class,99,bundle,true);
            }
        });
        mBinding.name.setOnClickListener(v -> startActivityForResult(IntegralMallActivity.class,0,null,true));
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<List<Exchange>>get(AppConfig.Goodsrecord)
                .tag(this)
                .params("pageNumber",pageNumber)
                .params("pageSize",20)
                .execute(new DialogCallback<List<Exchange>>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<List<Exchange>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            if (isOk(response)){
                                showData();
                                mBinding.rv.setVisibility(View.VISIBLE);
                                priceAdapter.setNewData(response.body());
                            }else {
                                showErrorView(response.code());
                            }
                        }else{
                            mBinding.vEmpty.setVisibility(View.VISIBLE);
                            mBinding.rv.setVisibility(View.GONE);
                            showData();
                        }
                    }

                    @Override
                    public void onError(com.lzy.okgo.model.Response<List<Exchange>> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==99){
            loadData(true);
        }
    }
}
